package source

import (
	"data-transfer/conf"
	"data-transfer/global"
)

type MysqlSource struct {
}

func (m *MysqlSource) Config() *conf.DbConfig {

	pluginName := global.GViper.GetString("plugin_name")
	mysqlDumpExecutionPath := global.GViper.GetString("mysql_dump_execution_path")
	monitorTables := global.GViper.GetStringSlice("monitor_tables")
	configMap := global.GViper.GetStringMapString("db")

	dbConfig := new(conf.DbConfig)

	dbConfig.PluginName = pluginName
	dbConfig.MysqlDumpExecutionPath = mysqlDumpExecutionPath
	dbConfig.MonitorTables = monitorTables
	dbConfigMap := make(map[string]*conf.DbConfInfo)

	for k, _ := range configMap {

		dbConfInfo := new(conf.DbConfInfo)

		dbConfInfo.DbName = k

		addrKey := "db" + "." + k + ".connect_addr"
		maxIdleConnsKey := "db" + "." + k + ".max_idle_conns"
		maxOpenConnsKey := "db" + "." + k + ".max_open_conns"

		dbConfInfo.ConnectAddr = global.GViper.GetString(addrKey)
		dbConfInfo.MaxIdleConns = global.GViper.GetInt(maxIdleConnsKey)
		dbConfInfo.MaxOpenConns = global.GViper.GetInt(maxOpenConnsKey)

		dbConfigMap[k] = dbConfInfo

	}

	dbConfig.DB = dbConfigMap
	return dbConfig

}
